//
//  AddCityViewController.m
//  YaHooFrame
//
//  Created by dg11185_zal on 14/11/25.
//  Copyright (c) 2014年 dg11185. All rights reserved.
//

#import "AddCityViewController.h"

@interface AddCityViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *resultArray;//搜索结果
    UITableView *myBeaconsTableView;//列表视图
}

@end

@implementation AddCityViewController

@synthesize delegate;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    resultArray = [NSMutableArray array];
    
    //显示主视图
    [self showMainView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

//主视图
-(void) showMainView{
    //添加搜索框
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 44)];
    //searchBar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
    searchBar.delegate = self;
    searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    searchBar.placeholder = @"输入城市名";
    searchBar.showsCancelButton = YES;
    searchBar.barStyle = UIBarStyleBlackTranslucent;
    searchBar.keyboardType = UIKeyboardTypeDefault;
    [self.view addSubview:searchBar];
    
    [searchBar becomeFirstResponder];
    
    //添加列表视图
    myBeaconsTableView = [[UITableView alloc]
                                       initWithFrame:CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height-44)
                                       style:UITableViewStylePlain];
    myBeaconsTableView.backgroundColor = [UIColor clearColor];
    myBeaconsTableView.delegate=self;
    myBeaconsTableView.dataSource=self;
    myBeaconsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:myBeaconsTableView];
}


#pragma mark -Delegate
//点击搜索按钮
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog( @"%s,%d" , __FUNCTION__ , __LINE__ );
}
//点击取消按钮
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    NSLog( @"%s,%d" , __FUNCTION__ , __LINE__ );
    [searchBar resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:nil];
}
//文本域改变
-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if (searchText.length>0) {
        [resultArray addObject:searchText];
        [myBeaconsTableView reloadData];
    }
}

/******UITableViewDataSource******/
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return resultArray.count;
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    
    cell.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.9];
    cell.textLabel.text = [resultArray objectAtIndex:indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    
    //分割线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 49, tableView.frame.size.width-10, 1)];
    lineView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.3];
    [cell.contentView addSubview:lineView];
    
    return cell;
}
//选中cell
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.delegate) {
        NSString *cityName = [resultArray objectAtIndex:indexPath.row];
        [self.delegate addCitySuccess:cityName];
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}



@end
